// 公用部分，公用参数
var http_url = "http://t.hnqingyun.com/bsxt/bsxt_app/public/index.php/api/";
var http_url_src = "http://t.hnqingyun.com/bsxt/";
sessionStorage.token = 1234567;
var imgBase64 = '';
var imgType ='';
var curPage = 1;
var totalPage;


// 阻止button默认点击事件
$('.btn').click(function(e){
    e.preventDefult();

});

// 时间戳转化为标准时间
function timestampToTime(timestamp) {

    // 时间戳为10位需*1000，时间戳为 13位的话不需要*1000
    var date = new Date(timestamp * 1000);
    var Y = date.getFullYear() + '-';
    var M = date.getMonth()+1 + '-';
    var D = date.getDate() + ' ';
    var h = date.getHours() + ':';
    var m = date.getMinutes() + ':';
    var s = date.getSeconds();
    return Y + M + D + h + m +s;
}


// 新增按钮点击事件

function addBtnAction(pageClickAction) {
    $('.add').click(function () {
       var id = $(this).prop('id');
       console.log(id);
        // 通过自定义属性获取菜单栏的名字
        // content = $(this).prop('name');

        // 通过字符串拼接获得 id 为 'add' + id 的项目内的文字
        var item_name = $('#add'+ id).children(".item_name").html();

        // console.log(item_name);
        // 改变上面路径的内容
        $('.content_item_name').html(item_name);

        // 加载新增/编辑内容的页面
        $('#content_wrap').load('add'+id+'_wy.html',function () {
        eval('add' + id +'()');
        });
    });

}
// 搜索按钮点击事件
function queryBtnAction(pageClickAction) {
    // 搜索按钮点击事件
    $('.query').click(function () {
        // 重新加载列表
        pageClickAction();
    });
}



// 改变轮播图上下架功能，改变学员、主讲老师状态功能，还有删除功能
// 封装的函数需要三个参数，第一个参数为jQuery对象，传递一个元素
// 第二个参数为接口的地址+ "?token=" + sessionStorage.token + "&id="，
// 第三个参数为回调函数，传递一个函数名称，在下面要要用到的函数
function stateDelete(ele, http_url_state, func) {
    ele.click(function () {

        $.ajax({
            url: http_url_state + $(this).parents('tr').prop('tid'),
            post: 'GET',
            success: function (response) {
                console.log(response);
                if (response.code == 0) {
                    func();
                } else {
                    alert('操作失败');
                }
            }
        })

    });
}


function resetPassword() {
    $('.reset').click(function () {

        console.log(11111);
        console.log($(this).parents('tr').prop('tid'));
        $.ajax({
            url: http_url + 'Admin/reset_password',
            post: 'POST',
            data: {
                token: sessionStorage.token,
                id : $(this).parents('tr').prop('tid'),
                old_password: $('#old_password').val(),
                password: $('#password').val(),
                confirm_password: $('#confirm_password').val()
            },
            success: function (response) {
                console.log(response);
                if (response.code == 0) {
                    func();
                } else {
                    alert('失败');
                }
            }
        })

    });
}




//获取辅导老师列表
/*
function teacherList() {

   var httpUrl = http_url + 'Team/coach_teacher_list?token=' + sessionStorage.token;
   $.get(httpUrl ,function (response){
      console.log('正在获取辅导老师列表');
      // console.log(response);
      if (response.code == 0) {
         showTeacherList(response);
      } else {
         return alert ('辅导老师列表获取失败');
      }
   });
   function showTeacherList(response) {
      var list = response.data;
      console.log(list);
      list.forEach(function (list, index) {
         var option =  $('<option/>').prop('value',list.id).html(list.name);
         option.appendTo($('#coach_teacher_id'));
      });
   }
}
*/



// 获取主讲老师列表

function speakTeacherList() {
    $.ajax({
        url: http_url + 'Live/teacher_list?token=' + sessionStorage.token,
        type: 'GET',
        success: function (response) {
            console.log('正在获取辅导老师列表');
            // console.log(response);
            if (response.code == 0) {
                showSpeakTeacherList(response);
            } else {
                return alert('辅导老师列表获取失败');
            }
        }
    });
    function showSpeakTeacherList(response) {
        var list = response.data;
        console.log(list);
        list.forEach(function (list, index) {
            var option =  $(' <option value = "' + list.id + '">' + list.name + '</option>');
            option.appendTo($('#speak_teacher_id'));
        });
    }
}




// 获取辅导老师列表

function teacherList() {
    $.ajax({
        url: http_url + 'Team/coach_teacher_list?token=' + sessionStorage.token,
        type: 'GET',
        success: function (response) {
            console.log('正在获取辅导老师列表');
            // console.log(response);
            if (response.code == 0) {
                showTeacherList(response);
            } else {
                return alert('辅导老师列表获取失败');
            }
        }
    });
    function showTeacherList(response) {
        var list = response.data;
        console.log(list);
        list.forEach(function (list, index) {
            var option =  $(' <option value = "' + list.id + '">' + list.name + '</option>');
            option.appendTo($('#coach_teacher_id'));
        });
    }
}


// 获取年级列表
function gradeList() {
    $.ajax({
        url: http_url + 'Coachteacher/grade_list?token=' + sessionStorage.token,
        type: 'GET',
        success: function (response) {
            console.log('正在获取年级列表');
            // console.log(response);
            if (response.code == 0) {
                showGradeList(response);
            }
        }
    });
    function showGradeList(response) {
        var list = response.data;
        // console.log(list);
        list.forEach(function (list, index) {
            var option =  $(' <option value = "' + list.id + ' " > ' + list.grade+ '</option>');
            option.appendTo($('#grade_id'));
        });
    }

}



// 获取学科/专业列表
function typeList() {
    console.log('正在获取专业列表');
    $.ajax({
        url: http_url + 'Course/type_list?token=' + sessionStorage.token,
        type: 'GET',
        success: function (response) {
            console.log('正在获取专业列表');
            // console.log(response);
            if (response.code == 0) {
                // console.log(response);
                showGradeList(response);
            }
        }
    });
    function showGradeList(response) {
        var list = response.data;
        // console.log(list);
        list.forEach(function (list, index) {
            var option =  $(' <option value = "' + list.id + ' " > ' + list.type+ '</option>');
            option.appendTo($('#type_id'));
        });
    }

}

// 获取班级列表
function teamList() {

    $.ajax({
        url: http_url + 'Student/team_list?token=' + sessionStorage.token,
        type: 'GET',
        success: function (response) {
            console.log('正在获取班级列表');
            // console.log(response);
            if (response.code == 0) {
                showGradeList(response);
            }
        }
    });
    function showGradeList(response) {
        var list = response.data;
        // console.log(list);
        list.forEach(function (list, index) {
            var option =  $(' <option value = "' + list.id + ' " > ' + list.team+ '</option>');
            option.appendTo($('#team_id'));
        });
    }

}


// 选择年级获取班级
function gradeTeamList() {
    $('#grade_id').change(function () {
        $.ajax({
            url: http_url + 'Team/number_list?token=' + sessionStorage.token + '&grade_id=' + $('#grade_id').val(),
            type: 'GET',
            success: function (response) {
                console.log('正在获取'+ $('#grade_id').html() +'班级列表');
                // console.log(response);
                if (response.code == 0) {
                    showGradeTeamList(response);
                }
            }
        });
        function showGradeTeamList(response) {
            var list = response.data;
            console.log(list);
            $('#team_id').find('option').remove();
            // console.log(list);
            list.forEach(function (list, i) {
                var option =  $(' <option value = '+ list + '>' + list+ '</option>');
                option.appendTo($('#team_id'));
            });
        }

    });

}

// 获取角色列表 

function roleList() {
    $.ajax({
        url: http_url + 'Admin/role?token=' + sessionStorage.token,
        type: 'GET',
        success: function (response) {
            console.log('正在获取角色列表');
            // console.log(response);
            if (response.code == 0) {
                showRoleList(response);
            }
        }
    });
    function showRoleList(response) {
        var list = response.data;
        // console.log(list);
        list.forEach(function (list, index) {
            var option =  $(' <option value = "' + list.id + ' " > ' + list.title+ '</option>');
            option.appendTo($('#role_id'));
        });
    }

}


// 分页器调用
function createPagiantion(response, pageClickAction ) {

    var info = response.data;
    Pagination({
        ele: '#ele',
        totalCount: info.total,
        totalPage: info.last_page,
        currentPage: info.current_page,
        needTotalCount: true,

        callback: function (page, flag) {
            if (flag) {
                curPage = page;
                pageClickAction();
                console.log('当前是第' + page + ' 页')
            }

        }
    });
}

//分页器
function Pagination (data) {
    var isInit = true;
    var ele = data.ele;
    $(ele + '>div').html('');
    var currentPage = data.currentPage ? data.currentPage : 1;
    var callback = data.callback ? data.callback : function(pageNum){
        // console.log('当前是第' + pageNum + '页');
    };
    // 跳转指定页函数
    function inputPage () {
        if(!isInit){
            if(currentPage == $(ele + ' input').val()) return $(ele + ' input').val('');
            currentPage = $(ele + ' input').val();
        }
        if(($(ele + ' input').val() > 9 && data.totalPage > 9) || (isInit && currentPage > 10)){
            if(currentPage%10 < 5){
                var a = (currentPage - currentPage%10)/10;
                var b = a - 1;
                var c = a + b;
                $(ele + ' ul').animate({left: -160 *c});
                if(currentPage - (Math.floor(data.totalPage/10)*10) >= 0 && currentPage - (Math.floor(data.totalPage/10)*10) <= 5){
                    if(data.totalPage%10 >= 5 && data.totalPage - currentPage <= data.totalPage - Math.floor(data.totalPage/10)*10 - 5){
                        $(ele + ' .list_box').animate({width: (data.totalPage - Math.floor(data.totalPage/10)*10 +5)*32});
                    } else if(data.totalPage%10 < 5 && data.totalPage - currentPage <= data.totalPage - Math.floor(data.totalPage/10)*10){
                        $(ele + ' .list_box').animate({width: (data.totalPage - Math.floor(data.totalPage/10)*10 + 5)*32});
                    } else {
                        $(ele + ' .list_box').width(320);
                    }
                } else {
                    $(ele + ' .list_box').animate({width: 320});
                }
            } else {
                $(ele + ' ul').animate({left: -160 *((currentPage - currentPage%10)/5)});
                if(currentPage - (Math.floor(data.totalPage/10)*10+5) >= 0 && currentPage - (Math.floor(data.totalPage/10)*10+5) <= 5){
                    $(ele + ' .list_box').animate({width: (data.totalPage - Math.floor(data.totalPage/10)*10)*32});
                } else if(currentPage - (Math.floor(data.totalPage/10)*10) >= 0 && currentPage - (Math.floor(data.totalPage/10)*10) <= 5){
                    $(ele + ' .list_box').animate({width: (data.totalPage - Math.floor(data.totalPage/10)*10)*32});
                } else {
                    $(ele + ' .list_box').animate({width: 320});
                }
            }

        } else {
            $(ele + ' ul').animate({left: 0});
            $(ele + ' .list_box').animate({width: 320});
        }
        $(ele + ' input').val('');
        $(ele + ' ul li').eq(currentPage-1).addClass('Pagination_active').siblings().removeClass('Pagination_active');
        callback(currentPage);
    }
    if(data.needTotalCount){
        // $(ele + ' .Pagination').append('<span class="total_num">共<i></i>条</span>');
        // $(ele +' .Pagination .total_num i').text(data.totalCount);
    }
    var PaginationLiList = '';
    if(data.totalCount != 0){
        $(ele + ' .Pagination').append(
            // '<span class="first_page">首页</span>' +
            // '<span class="prepage">上一页</span>' +
            '<span class="prepage"><</span>' +
            '<div class="list_box clearfix"><ul></ul></div>' +
            '<span class="nextpage">></span>'

            // '<span class="nextpage">下一页</span>' +
            // '<span class="last_page">尾页</span>' +
            // '<span class="total_page">共'+data.totalPage+'页</span>' +
            //     '<input type="text">' +
            // '<span class="redir">跳转</span>'
        );
        if(data.totalPage < 10){
            $(ele + ' .Pagination .list_box').width(data.totalPage*32);
        }
        inputPage();
        isInit = false;
        for(var i = 0; i < data.totalPage; i++){
            var j = i + 1;
            if(i==currentPage-1){
                var li = '<li class="Pagination_active" data-page="'+ j +'">' + j + '</li>';
            } else {
                var li = '<li data-page="'+ j +'">' + j + '</li>';
            }
            PaginationLiList += li;
        }
        $(ele + ' .Pagination ul').html('');
        $(ele + ' .Pagination ul').html(PaginationLiList);
        $(ele + ' .Pagination ul').width(data.totalPage*32);

        // 页码点击跳到指定页数
        $(ele + ' .Pagination ul').on('click','li',function(){
            currentPage = $(this).data("page");
            if(!$(this).hasClass('Pagination_active')){
                $(this).addClass('Pagination_active').siblings().removeClass('Pagination_active');
                if(((currentPage/10)%1==0 || ((currentPage/5)%1==0 && currentPage > 5)) && currentPage != data.totalPage){
                    if(currentPage%2==0){
                        var a = currentPage/10;
                        var b = a - 1;
                        var c = a + b;
                        $(ele + ' .Pagination ul').animate({left: -160 *c});
                    } else {
                        $(ele + ' .Pagination ul').animate({left: -160 *((currentPage/5)-1)});
                    }
                    if(data.totalPage - currentPage < 5){
                        $(ele + ' .Pagination .list_box').animate({width: (data.totalPage - currentPage + 5)*32});
                    }
                }
                if(((currentPage-1)/5)%1==0 && currentPage > 5 && currentPage != data.totalPage){
                    $(ele + ' .Pagination ul').animate({left: -160 *(((currentPage-1)/5)-1)});
                    if(data.totalPage - currentPage >= 5){
                        $(ele + ' .Pagination .list_box').animate({width: 320});
                    }
                }
                callback(currentPage,true);
            }
        });
        // 跳到第一页
        /*
        $(ele + ' .Pagination .first_page').on('click',function(){
            if(currentPage != 1){
                $(ele + ' .Pagination ul li').eq(0).addClass('Pagination_active').siblings().removeClass('Pagination_active');
                $(ele + ' .Pagination ul').animate({left: 0});
                if(data.totalPage > 9){
                    $(ele + ' .Pagination .list_box').animate({width: 320});
                } else {
                    $(ele + ' .Pagination .list_box').animate({width: data.totalPage*32});
                }
                currentPage = 1;
                callback(currentPage, true);
            }
        });
        // 跳到最后一页
        $(ele + ' .Pagination .last_page').on('click',function(){
            if(currentPage != data.totalPage){
                currentPage = data.totalPage;
                $(ele + ' .Pagination ul li').eq(data.totalPage-1).addClass('Pagination_active').siblings().removeClass('Pagination_active');
                if(data.totalPage > 10){
                    if(data.totalPage%10 <= 5){
                        var a = (data.totalPage - data.totalPage%10)/10;
                        var b = a - 1;
                        var c = a + b;
                        $(ele + ' .Pagination ul').animate({left: -160 *c});
                        $(ele + ' .Pagination .list_box').stop().animate({width: (data.totalPage-Math.floor(data.totalPage/10)*10+5)*32});
                    } else {
                        $(ele + ' .Pagination ul').animate({left: -160 *((data.totalPage - data.totalPage%10)/5)});
                        $(ele + ' .Pagination .list_box').stop().animate({width: (data.totalPage-Math.floor(data.totalPage/10)*10)*32});
                    }
                }
                callback(currentPage, true);
            }
        });*/
        // 上一页
        $(ele + ' .Pagination .prepage').on('click',function(){
            if(currentPage > 1){
                currentPage--;
                if(((currentPage-1)/5)%1==0 && currentPage > 5){
                    $(ele + ' .Pagination ul').animate({left: -160 *(((currentPage-1)/5)-1)});
                    if(data.totalPage - currentPage >= 4){
                        $(ele + ' .Pagination .list_box').animate({width: 320});
                    }
                }
                $(ele + ' .Pagination ul li').eq(currentPage-1).addClass('Pagination_active').siblings().removeClass('Pagination_active');
                callback(currentPage, true);
            }
        });
        // 下一页
        $(ele + ' .Pagination .nextpage').on('click',function(){
            if(currentPage < data.totalPage){
                if(currentPage > 9){
                    if(((currentPage/10)%1==0 || ((currentPage/5)%1==0 && currentPage > 5)) && currentPage != data.totalPage){
                        if(currentPage%2==0){
                            var a = currentPage/10;
                            var b = a - 1;
                            var c = a + b;
                            $(ele + ' .Pagination ul').animate({left: -160 *c});
                        } else {
                            $(ele + ' .Pagination ul').animate({left: -160 *((currentPage/5)-1)});
                        }
                        if(data.totalPage - currentPage < 5){
                            $(ele + ' .Pagination .list_box').animate({width: (data.totalPage - currentPage + 5)*32});
                        }
                    }
                }
                currentPage++;

                $(ele + ' .Pagination ul li').eq(currentPage-1).addClass('Pagination_active').siblings().removeClass('Pagination_active');
                callback(currentPage, true);
            }
        });
        // 输入跳转指定页
        $(ele + ' .Pagination input').on('keydown',function(e){
            if(e.keyCode == 13 && $(this).val() <= data.totalPage && $(this).val() != 0){
                inputPage();
            }
        });
        // 转跳按钮
        $(ele + ' .Pagination .redir').on('click', function(){
            if($(ele + ' .Pagination input').val() <= data.totalPage && $(ele + ' input').val() != 0){
                inputPage();
            }
        });
    }
}
